package com.acxy.ants.core.handler;

import com.acxy.ants.core.Handler;
import com.acxy.ants.common.kit.StrKit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-04-27
 */
public class ContextPathHandler implements Handler {

    private String contextPathName;

    public ContextPathHandler() {
        contextPathName = "ctx";
    }

    public ContextPathHandler(String contextPathName) {
        if (StrKit.isBlank(contextPathName)) {
            throw new IllegalArgumentException("contextPathName can not be blank.");
        }
        this.contextPathName = contextPathName;
    }

    @Override
    public boolean preHandle(String target, HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute(contextPathName, request.getContextPath());
        return true;
    }
}
